//
//  ThreeViewController.m
//  News
//
//  Created by weige on 16/6/6.
//  Copyright © 2016年 weige. All rights reserved.
//

#import "ThreeViewController.h"
#import "ShopCartMarkerView.h"
#import "ShopCartTableViewCell.h"
@interface ThreeViewController ()<UITableViewDataSource,UITableViewDelegate>
{

    UIImageView *emptyImage;
    UILabel *emptyLable;
    UIButton *emptyButton;
    ReceiptAdressView * adressView;
    UIView *tableHeader;
    BOOL isFristLoad;
    ShopCartMarkerView *shopCartmarket;
    UITableView *supperMarketTableView;
}
@end

@implementation ThreeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addNotifiction];
    
    self.view.backgroundColor=[UIColor whiteColor];
   
    NSLog(@"%lu",(unsigned long)[ShopCarTools sharInstance].goodArr.count);
    for (Goods *g in [ShopCarTools sharInstance].goodArr) {
        NSLog(@"%d",g.userBuyNumber);
    }
    NSLog(@"%@",[ShopCarTools sharInstance].goodArr.description);
    [self bulidEnptyUI];
    supperMarketTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64-50) style:UITableViewStylePlain];
   
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if ([ShopCarTools sharInstance].goodArr.count==0) {
        
        [self showProductEnptyUI];
    }
    else
    {
        emptyButton.hidden=NO;
        emptyImage.hidden=NO;
        emptyLable.hidden=NO;
        supperMarketTableView.hidden=NO;
         isFristLoad=NO;
        [self showProductUILoad];
        [supperMarketTableView reloadData];
    }
}



-(void)showProductEnptyUI
{
    emptyButton.hidden=NO;
    emptyImage.hidden=NO;
    emptyLable.hidden=NO;
    supperMarketTableView.hidden=YES;
    isFristLoad=NO;
}

-(void)showProductUILoad
{
    if (!isFristLoad) {
        [self bulidTableHeader];
        [self buildSupermarketTableView];
        
        isFristLoad=YES;
    }
}

-(void)buildSupermarketTableView
{
    supperMarketTableView.tableHeaderView=tableHeader;
    supperMarketTableView.dataSource=self;
    supperMarketTableView.delegate=self;
    [self.view addSubview:supperMarketTableView];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  [ShopCarTools sharInstance].goodArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ShopCartTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"mycell"];
    if (cell==nil) {
        
        cell=[[ShopCartTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"mycell"];
    }
    cell.good=[ShopCarTools sharInstance].goodArr[indexPath.row];
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}
/*!
 
 *  @author weige, 16-06-12 14:06:04
 *
 *  @brief 创建收件信息
 */
-(void)bulidTableHeader
{
    tableHeader=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 243)];
    tableHeader.backgroundColor=[UIColor lightGrayColor];
    
    [self.view addSubview:tableHeader];
    adressView = [[ReceiptAdressView alloc]initWithFrame:CGRectMake(0, 10, self.view.frame.size.width, 70)];
    [tableHeader addSubview:adressView];
    shopCartmarket=[[ShopCartMarkerView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(adressView.frame)+10, self.view.frame.size.width, 50)];
    [tableHeader addSubview:shopCartmarket];
    [self createTimeUI];
    
}

/*!
 *  @author weige, 16-06-12 15:06:12
 *
 *  @brief
 */
-(void)createTimeUI
{
    UIView *timeView=[[UIView alloc]init];
    timeView.backgroundColor=[UIColor whiteColor];
    UILabel *signTimeTitleLabel=[[UILabel alloc]init];
    
    signTimeTitleLabel.text = @"收货时间";
    signTimeTitleLabel.textColor = [UIColor blackColor];
    signTimeTitleLabel.font = [UIFont systemFontOfSize:15];
    [signTimeTitleLabel sizeToFit];
    signTimeTitleLabel.frame = CGRectMake(15, 20, ReceiptSize(signTimeTitleLabel.text, 15).width, ReceiptSize(signTimeTitleLabel.text, 15).height);
    [timeView addSubview:signTimeTitleLabel];
    
    timeView.frame=CGRectMake(0, CGRectGetMaxY(shopCartmarket.frame)+1, self.view.frame.size.width, 50);
    [tableHeader addSubview:timeView];
    
    UILabel *signTimeLabel=[[UILabel alloc]init];
    signTimeLabel.textColor = [UIColor redColor];
    signTimeLabel.font = [UIFont systemFontOfSize:15];
    signTimeLabel.text = @"闪电送,及时达";
    signTimeLabel.frame=CGRectMake(CGRectGetMaxX(signTimeTitleLabel.frame)+30, 20, ReceiptSize(signTimeLabel.text, 15).width, ReceiptSize(signTimeLabel.text, 15).height);
    [timeView addSubview:signTimeLabel];

    UILabel *reserveLabel=[[UILabel alloc]init];
    
    
    reserveLabel.text = @"可预定";
    reserveLabel.backgroundColor = [UIColor whiteColor];
    reserveLabel.textColor = [UIColor redColor];
    reserveLabel.font = [UIFont systemFontOfSize:15];
    reserveLabel.frame = CGRectMake(SCREEN_WIDTH - 90, 20, ReceiptSize(reserveLabel.text, 15).width, ReceiptSize(reserveLabel.text, 15).height);
    
    [timeView addSubview:reserveLabel];
    
    UIImageView *arrowImageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_go"]];
    arrowImageView.frame=CGRectMake(SCREEN_WIDTH-15, 25, arrowImageView.frame.size.width, arrowImageView.frame.size.height);
    [timeView addSubview:arrowImageView];
    
    UIView *mark=[[UIView alloc]init];
    mark.backgroundColor=[UIColor whiteColor];
    mark.frame=CGRectMake(0, CGRectGetMaxY(timeView.frame)+1, SCREEN_WIDTH, 50);
    [tableHeader addSubview:mark];
    UILabel *labmarker=[[UILabel alloc]init];
    labmarker.text=@"收货备注";
    labmarker.textColor = [UIColor blackColor];
    labmarker.font = [UIFont systemFontOfSize:15];
    [labmarker sizeToFit];
    labmarker.frame = CGRectMake(15, 20, ReceiptSize(labmarker.text, 15).width, ReceiptSize(labmarker.text, 15).height);
    [mark addSubview:labmarker];
    UITextField *marktext=[[UITextField alloc]init];
    marktext.placeholder = @"可输入100字以内特殊要求内容";
    marktext.frame = CGRectMake(CGRectGetMaxX(labmarker.frame) + 10, 10, SCREEN_WIDTH - CGRectGetMaxX(labmarker.frame) - 10 - 20,   30);
    marktext.font = [UIFont systemFontOfSize:15];
    marktext.borderStyle = UITextBorderStyleRoundedRect;
    marktext.autocorrectionType = UITextAutocorrectionTypeNo;
    marktext.autocapitalizationType = UITextAutocapitalizationTypeNone;
    [mark addSubview:marktext];

}


/*!
 *  @author weige, 16-06-12 14:06:38
 *
 *  @brief 购物车没有商品时显示的UI
 */
-(void)bulidEnptyUI
{
    emptyImage=[[UIImageView alloc]init];
  
    emptyImage.image=[UIImage imageNamed:@"v2_shop_empty"];
    emptyImage.contentMode=UIViewContentModeCenter;
    emptyImage.frame=CGRectMake((self.view.frame.size.width-emptyImage.frame.size.width)*0.5, self.view.frame.size.height*0.25, emptyImage.frame.size.width, emptyImage.frame.size.height);
    emptyImage.hidden=YES;
    [self.view addSubview:emptyImage];
    
    UIBarButtonItem *barbtn=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(aa:)];
    self.navigationItem.leftBarButtonItem=barbtn;
    emptyLable=[[UILabel alloc]init];
    emptyLable.frame=CGRectMake(0, CGRectGetMaxX(emptyImage.frame)+10, self.view.frame.size.width, 50);
    emptyLable.textAlignment=NSTextAlignmentCenter;
    emptyLable.text=@"亲,购物车空空的耶~赶紧挑好吃的吧";
    emptyLable.textColor=[UIColor lightGrayColor];
    emptyLable.font=[UIFont systemFontOfSize:12];
    emptyLable.hidden=YES;
    [self.view addSubview:emptyLable];
    
    emptyButton=[UIButton buttonWithType:UIButtonTypeCustom];
    emptyButton.frame=CGRectMake((self.view.frame.size.width-150)/2, CGRectGetMaxY(emptyLable.frame)+15, 150, 30);
    [emptyButton setTitle:@"去逛逛" forState:UIControlStateNormal];
    [emptyButton setBackgroundImage:[UIImage imageNamed:@"btn.png"] forState:UIControlStateNormal];
    [emptyButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    emptyButton.hidden=YES;
    [self.view addSubview:emptyButton];
    

    
}
-(void)aa:(UIBarButtonItem *)sender
{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)addNotifiction
{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(goodsubtractionclick:) name:@"goodsubtraction" object:nil];
    
}
-(void)goodsubtractionclick:(NSNotification *)sender
{
    [supperMarketTableView reloadData];
    if ([ShopCarTools sharInstance].goodArr.count==0) {
        
        supperMarketTableView.hidden=YES;
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
